/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OPMViewerJFrame.java
 *
 * Created on Mar 9, 2011, 7:57:11 PM
 */
package org.opmviewer.jframes;

import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import org.opmviewer.converters.Neo4J2Prefuse;
import org.opmviewer.core.views.ForceDirectedView;
import org.opmviewer.core.views.GraphvizView;
import org.opmviewer.core.views.RadialView;
import prefuse.data.Graph;

/**
 *
 * @author grzegorz
 */
public class OPMViewerJFrame extends javax.swing.JFrame {

    Graph g;
    String layout;

    /** Creates new form OPMViewerJFrame */
    public OPMViewerJFrame() {
        initComponents();
        layout = "Layered DAG layout";
        //controls.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        visPanel = new javax.swing.JPanel();
        ovwPanel = new javax.swing.JPanel();
        layoutComboBox = new javax.swing.JComboBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        visualizeMenuItem = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));

        visPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Visualization"));
        visPanel.setLayout(new java.awt.BorderLayout());

        ovwPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Overview"));
        ovwPanel.setPreferredSize(new java.awt.Dimension(230, 230));
        ovwPanel.setLayout(new java.awt.BorderLayout());

        layoutComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Layered DAG layout", "Force directed layout", "Radial layout" }));
        layoutComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                layoutComboBoxActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        visualizeMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        visualizeMenuItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Visualize_menu_icon.jpg"))); // NOI18N
        visualizeMenuItem.setText("Visualize");
        visualizeMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                visualizeMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(visualizeMenuItem);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(visPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 681, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addComponent(ovwPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(layoutComboBox, 0, 309, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(visPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ovwPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(layoutComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void runLayout() {
        final OPMViewerJFrame frame = this;
        SwingWorker worker = new SwingWorker<Void, Void>() {

            @Override
            protected Void doInBackground() throws Exception {
                try {
                    visPanel.removeAll();
                    String label = "IF (LENGTH([VALUE]) > 20) THEN"
                            + " CONCAT(LEFT([VALUE], 20), '...') ELSE [VALUE]";
                    if(layout.equals("Layered DAG layout")) {
                        GraphvizView.demo(g, label, visPanel, ovwPanel);
                    }
                    else if(layout.equals("Force directed layout")){
                        ForceDirectedView.demo(g, label, visPanel, ovwPanel);
                    }
                    else {
                        RadialView.demo(g, label, visPanel, ovwPanel);
                    }

                    pack();
                    repaint();
                } catch (Exception e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(frame,
                            "Error while trying to display provenance!", "ERROR",
                            JOptionPane.ERROR_MESSAGE);
                }
                return null;
            }
        };

        worker.execute();
    }

    private void visualizeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_visualizeMenuItemActionPerformed
        final DBSelectJFrame dbsel = new DBSelectJFrame(this, true);
        if (!dbsel.selected()) {
            return;
        }
        dbsel.savePrefs();
        Neo4J2Prefuse neo2pref = new Neo4J2Prefuse();
        g = neo2pref.constructGraph(dbsel.getSelectedPath()
                + System.getProperty("file.separator")
                + dbsel.getSelectedName());
        runLayout();
    }//GEN-LAST:event_visualizeMenuItemActionPerformed

    private void layoutComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_layoutComboBoxActionPerformed
        layout = (String) layoutComboBox.getSelectedItem();
        visPanel.removeAll();
        ovwPanel.removeAll();
        runLayout();
    }//GEN-LAST:event_layoutComboBoxActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new OPMViewerJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JComboBox layoutComboBox;
    private javax.swing.JPanel ovwPanel;
    private javax.swing.JPanel visPanel;
    private javax.swing.JMenuItem visualizeMenuItem;
    // End of variables declaration//GEN-END:variables
}
